package com.zzty.springmybatis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzty.springmybatis.entity.ClassGroup;
import com.zzty.springmybatis.entity.User;
import com.zzty.springmybatis.service.GroupService;;

@Controller
@RequestMapping("/group")
public class GroupController {
	@Autowired
	private GroupService GroupService;

	@RequestMapping("/group")
	public String showAll(HttpServletRequest request, Model model) {
		 User user = (User) request.getSession().getAttribute("User");  		
		 String clazzId=String.valueOf(user.getClazzId());
		 
		
		 //String clazzId="4";
		 List<User> all=this.GroupService.selectall(clazzId);
		 model.addAttribute("all", all);
		 int num=all.size();
		 double test=(double)num/(double)2;
		 if(test>num/2){
			 num=num/2+1;
			 
		 }else{
			 num=num/2;

			 
		 }
		 model.addAttribute("Num", num);
		
		 return "grouping";

	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public void insert(HttpServletRequest request, Model model) {
		 User user = (User) request.getSession().getAttribute("User");  		
		 Integer clazzId=user.getClazzId();
		 
		
		 //Integer clazzId=4;
		 this.GroupService.delete(clazzId);
		 String group=request.getParameter("gruop");
		 System.out.println(group);
		 ClassGroup g=new ClassGroup(clazzId,group);
		 this.GroupService.insertGroup(g);
		

	}
	
	
}
